<template>
  <a-config-provider :locale="locale">

    <div id="app">
      <keep-alive v-if="isLogin">
        <router-view v-if="$route.meta.alive" />
      </keep-alive>
      <router-view v-if="!$route.meta.alive || !isLogin" />

    </div>
  </a-config-provider>
</template>

<script>
import { domTitle, setDocumentTitle } from '@/utils/domUtil'
import { i18nRender } from '@/locales'
import { getUnread } from '@/api/system/message'
import { ACCESS_TOKEN, APP_TIME, CURRENT_PROJECT } from '@/store/mutation-types'
import store from '@/store'
import storage from 'store'
export default {
  data () {
    return {
      time: null,
      isLogin: false
    }
  },
  computed: {
    locale () {
      // 只是为了切换语言时，更新标题
      const { title } = this.$route.meta
      title && (setDocumentTitle(`${i18nRender(title)} - ${domTitle}`))

      return this.$i18n.getLocaleMessage(this.$store.getters.lang).antLocale
    }
  },
  mounted () {
    this.$globalVue.$on('unReadMsgListener', this.unReadMsgListener)
    window.addEventListener('click', this.handleKeyup)
  },
  destroyed () {
    this.$globalVue.$off('unReadMsgListener')
    window.removeEventListener('click', this.handleKeyup)
    this.logout()
  },
  watch: {
    $route (to, from) {
      this.isLogin = !!localStorage.getItem(ACCESS_TOKEN)
      const info = store.getters.userInfo
      if (this.isLogin && info && info.id && info.isAdmin === 0 && !info.idCard) {
        // 如果没有身份证 强制跳转到 用户信息完善页
        this.$router.push({ path: '/person' })
      } else if (this.isLogin && info && info.id && info.isRestPwd === 0) {
        // 强制跳转到 修改密码页面
        this.$router.push({ path: '/person', query: { type: 1 } })
      }
    }
  },
  methods: {
    handleKeyup (event) {
      storage.set(APP_TIME, Date.now(), 30 * 1000)
    },
    unReadMsgListener () {
      if (this.time == null) {
        if (localStorage.getItem(ACCESS_TOKEN)) {
          getUnread().then(res => {
            this.$globalVue.$emit('unReadMsgCount', res)
          })
        }
        this.time = setInterval(() => {
          if (localStorage.getItem(ACCESS_TOKEN)) {
            let proId = ''
            const pro = storage.get(CURRENT_PROJECT)
            if (pro != null && pro.length > 0) {
              const project = JSON.parse(pro)
              proId = project.id
            }
            getUnread({ proId: proId }).then(res => {
              this.$globalVue.$emit('unReadMsgCount', res)
            })
            const time = storage.get(APP_TIME)
            if (time) {
              const diff = Date.now() - time
              if (diff > 30 * 60 * 1000) {
                // 超过时间 退出
                this.$store.dispatch('Logout').then(() => {
                  this.$router.push({ name: 'login' })
                })
              }
            }
          } else {
            this.logout()
          }
        }, 20 * 1000)
      }
    },
    logout () {
      if (this.time) {
        clearInterval(this.time)
        this.time = null
      }
    }
  }
}
</script>
